/*
 *  Copyright 2009 Advanced Industrial Science and Technology (AIST).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file
 *  \brief
 */
#pragma once

namespace thrust {
  
  /*template <typename PA>
  struct property_traits
  {
    //typedef typename PA::value_type value_type;
    };*/

  //=================================
  // property_traits class
  template <typename PA>
  struct property_traits
  {
    typedef typename PA::key_type   key_type;
    typedef typename PA::value_type value_type;
    typedef typename PA::reference  reference;
    typedef typename PA::category   category;
  };
  
  /*template <typename T>
    struct property_traits<const T*> {
    typedef T value_type;
    };*/
  
  //=================================
  // property_traits category tags
  namespace detail {
    enum ePropertyMapID {
      READABLE_PA
      , WRITABLE_PA
      , READ_WRITE_PA
      , LVALUE_PA
      , OP_BRACKET_PA
      , RAND_ACCESS_ITER_PA
      , LAST_PA
    };
  }  // namespace detail
  
  struct readable_property_map_tag { enum { id =detail::READABLE_PA}; };
  struct writable_property_map_tag { enum { id =detail::WRITABLE_PA}; };
  struct read_write_property_map_tag :
    public readable_property_map_tag,
    public writable_property_map_tag { enum { id =detail::READ_WRITE_PA}; };
  struct lvalue_property_map_tag :
    public read_write_property_map_tag { enum { id =detail::LVALUE_PA}; };
  
  template <typename Reference, typename LValuePropertyMap>
  struct put_get_helper { };

  /*template <typename PropertyMap, typename Reference, typename K, typename CalledSpace>
    __host__ __device__
    //inline Reference
    inline void
    get( const put_get_helper<Reference,PropertyMap>& pa, const K& k, CalledSpace tag)
    {
    //Reference v =static_cast<const PropertyMap&>(pa)[k];
    //return v;
    //static_cast<const PropertyMap&>(pa)[k];
    get( pa, k, tag);
    }*/
  
  template <typename PropertyMap, typename Reference, typename K>
  __host__
  inline Reference
  get( const put_get_helper<Reference,PropertyMap>& pa, const K& k, 
       thrust::called_from_host_tag)
  {
    Reference v =static_cast<const PropertyMap&>(pa)[k];
    return v;
  }

  template <typename PropertyMap, typename Reference, typename K>
  __device__
  inline Reference
  get( const put_get_helper<Reference,PropertyMap>& pa, const K& k, 
       thrust::called_from_device_tag)
  {
    //Reference v =static_cast<const PropertyMap&>(pa)[k];
    //static_cast<const PropertyMap&>(pa)[k];
    const PropertyMap& p_ =static_cast<const PropertyMap&>(pa);
    return p_[k];
  }

  template <typename PropertyMap
            , typename Reference
            , typename K
            , typename V>
  __host__
  inline void
  put( put_get_helper<Reference,PropertyMap>& pa, K k, const V& v,
       thrust::called_from_host_tag)
  {
    PropertyMap& p =static_cast<PropertyMap&>(pa);
    p.put( k, v, thrust::called_from_host_tag());
  }
  
  template <typename PropertyMap
            , typename Reference
            , typename K
            , typename V>
  __device__
  inline void
  put( put_get_helper<Reference,PropertyMap>& pa, K k, const V& v,
       thrust::called_from_device_tag)
  {
    PropertyMap& p =static_cast<PropertyMap&>(pa);
    p.put( k, v, thrust::called_from_device_tag());
  }
  
  
  template <typename PropertyMap, typename Reference, typename K>
  __host__
  inline Reference
  get( const put_get_helper<Reference,PropertyMap>& pa, const K& k)
  {
    return get( pa, k, thrust::called_from_host_tag());
  }
  
  template <typename PropertyMap
            , typename Reference
            , typename K
            , typename V>
  __host__
  inline void
  put( put_get_helper<Reference,PropertyMap>& pa, K k, const V& v)
  {
    put( pa, k, v, thrust::called_from_host_tag());
  }
  
  //=======================================================================
  // A generalized identity property map
  template <typename T>
  struct typed_identity_property_map
    : public thrust::put_get_helper<T,typed_identity_property_map<T> >
  {
    typedef T key_type;
    typedef T value_type;
    typedef T reference;
    typedef thrust::readable_property_map_tag category;
    
  public:
    typed_identity_property_map() { std::cout <<"typed_identity_property_map()" <<std::endl;}
    inline value_type operator[]( const key_type& v) const { return v;}
  };

  //=======================================================================
  // A property map that applies the identity function to integer
  typedef typed_identity_property_map<std::size_t> identity_property_map;

} // namespace thrust
